package com.example.x.helpmeget2;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;

import com.alibaba.mobileim.YWIMKit;
import com.github.clans.fab.FloatingActionButton;

import form.AddressChoose;
import form.BuyAddress;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener

{

    FloatingActionButton f1,f2,f3;

    DrawerLayout drawer;
    Toolbar toolbar;
    private GestureDetector gd;


    TabLayout mTablayout;
    ViewPager mViewPager;
    TabLayout.Tab one,two,three,four;
    static YWIMKit mIMKit;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        setupDrawerContent(navigationView);






        mTablayout= (TabLayout) findViewById(R.id.tabLayout);
        mViewPager= (ViewPager) findViewById(R.id.viewPager);

        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            private String[] mTitles = new String[]{"主页", "订单", "查询"};

            @Override
            public Fragment getItem(int position) {

                if (position == 1) {
                    return new   TwoFragment();
                } else if (position == 2) {
                    return new ThreeFragment();
                }

                return  new OneFragment();
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }




        });



        mTablayout.setupWithViewPager(mViewPager);

        one = mTablayout.getTabAt(0);
        two = mTablayout.getTabAt(1);
        three = mTablayout.getTabAt(2);


        one.setIcon(getResources().getDrawable(R.drawable.index));
        two.setIcon(getResources().getDrawable(R.drawable.order));
        three.setIcon(getResources().getDrawable(R.drawable.search));






//        final String APP_KEY = "23332462";
//        final String userid = "test2";
//        mIMKit = YWAPI.getIMKitInstance(userid, APP_KEY);
//        String password = "test1";
//        IYWLoginService loginService = mIMKit.getLoginService();
//        YWLoginParam loginParam = YWLoginParam.createLoginParam(userid,password);
//        loginService.login(loginParam, new IWxCallback() {
//            @Override
//            public void onSuccess(Object... objects) {
////                            final String target = "test2";
////                            startActivity(mIMKit.getChattingActivityIntent(target, APP_KEY));
//
//                      }
//
//            @Override
//            public void onError(int i, String s) {
//                Toast.makeText(MainActivity.this, "login fail", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onProgress(int i) {
//
//            }
//        });


        init();
    }

    void init(){
        f1 = (FloatingActionButton) findViewById(R.id.fab_main_circle);
        f1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, BuyAddress.class));
            }
        });

    }

    private void switchToBook() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new BooksFragment()).commit();
        toolbar.setTitle("books");
    }

    private void switchToHello() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new hello()).commit();
        toolbar.setTitle("hello");
    }


    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {

                            case R.id.nav_address:
                               startActivity(new Intent(MainActivity.this,MainActivity.class));
                                break;
                            case R.id.nav_information:
                                switchToHello();
                                break;

                        }
                        menuItem.setChecked(true);
                        drawer.closeDrawers();
                        return true;
                    }
                });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_address) {
            // Handle the camera action
        } else if (id == R.id.nav_information) {

        } else if (id == R.id.nav_check_update) {

        } else if (id == R.id.nav_about_us) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }








}